<?php

namespace Shop\AdminBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Shop\AdminBundle\Controller\BaseController;

use Shop\AdminBundle\Entity\Shop;
use Shop\AdminBundle\Form\ShopType;

/**
 * @Route("/api")
 * @Method("GET")
 */
class ApiController extends BaseController
{
    /**
     * @Route("/")
     */
    public function defaultAction(){
        return $this->jsonResponse();
    }

    /**
     * @Route("/get_city_data")
     * @Method("GET")
     */
    public function getCityData(){
        $cityJsonData=$this->getConfData('city.json');
//        $cityJsonData=str_replace(array('\\n', '\\"'), '',$cityJsonData);

        $response= new Response();
        $response->setContent($cityJsonData)
            ->headers->set('Content-Type','application/json');
        return $response;

    }

    /**
     * @Route("/get_shop_by_brand")
     * @Method("GET")
     */
    public function getShopByBrand(Request $request){
        if($request->isXmlHttpRequest()){
            $bid=intval($request->query->get('bid'));
            $shopRepo=$this->getRepo('shop');

//            $queryShop=$shopRepo->findBy(['brandId'=> $bid]);
            $queryShop=$shopRepo->getByBrandId($bid);

            $ret=[];
            if(!empty($queryShop)){
                foreach($queryShop as $shop){
                    $ret[]=['id'=>$shop->getId(), 'name'=>$shop->getName()];
                }
            }

            return $this->jsonResponse(['result'=>$ret]);

        }

        return $this->jsonResponse();
    }

    /**
     * 获取品牌数据
     *
     * @param $brandId
     * @Route("/brand/{brandId}", name="api_brand", defaults={"brandId"=0} )
     * @Template("ShopAdminBundle:Demo:product_list.html.twig")
     *
     */
    public function getBrandList($brandId, Request $request){

        if(empty($brandId)){
            //TODO: 获取所有品牌下所有门店的门票列表
            /*
            $queryAllBrand=$repoBrand->getAll();
            if(!empty($queryAllBrand)){
                foreach($queryAllBrand as $brand){
                    $brandId=$brand->getId();
                    $brandName=$brand->getName();
                    $ret[$brandName]=[];
                }
            }
            */

            return new Response('brandId 不能为空');
        }

        /**@var \Shop\AdminBundle\Entity\BrandRepository $repoBrand */
        $repoBrand=$this->getRepo('brand');
        $ret=[];

        //TODO: 根据brandId获取该门票下门店的门票列表
        $brand=$repoBrand->find($brandId);

        if(empty($brand)){
            return new Response('对应id的品牌未找到');
        }

        $brandName=$brand->getName();
        $ret[$brandName]=[];

        /**@var \Shop\AdminBundle\Entity\ShopRepository $repoShop */
        $repoShop=$this->getRepo('shop');

        /**@var \Shop\AdminBundle\Entity\ProductRepository $repoProd */
        $repoProd=$this->getRepo('product');

        $shopList=$repoShop->getByBrandId($brand->getId());

        if(!empty($shopList)){
            foreach($shopList as $shop){
                $shopId=$shop->getId();
                $shopName=$shop->getName();
                $shopPic=$shop->getPicture();
                $shopAddress=$shop->getAddress();

                $shopProd=$repoProd->findBy(['shopId'=>$shopId]);
//                        $shopProd=$repoProd->getListByShop($shopId);

                $ret[$brandName][]=[
                    'shopId'=> $shopId,
                    'shopName'=>$shopName,
                    'shopPicture'=>$shopPic,
                    'shopAddress'=>$shopAddress,
                    'shopProducts'=> $shopProd
                ];

            }
        }

//        dump($ret[$brandName]);exit;

        return [
            'brand'=> $brandName,
            'shop_list'=> $ret[$brandName]
        ];

//        return $this->jsonResponse(['result'=>$ret]);

    }

    /**
     * 获取门店数据
     *
     * @param       $shopId
     * @Route("/shop/{shopId}", name="api_shop", defaults={"shopId"=0} )
     *
     */
    public function getShopList($shopId, Request $request){
        $shopRepo=$this->getRepo('shop');

        if(empty($shopId)){

        }
        else{
            dump($shopRepo);
        }

        exit;
    }

    /**
     * 获取门票数据
     *
     * @param       $prodId
     * @Route("/ticket/{prodId}", name="api_ticket", defaults={"prodId"=0} )
     * @Template("ShopAdminBundle:Demo:product_detail.html.twig")
     */
    public function getProductInfo($prodId, Request $request){
        if(empty($prodId)){
            return new Response('prodId 不能为空');
        }
        else{
            /**@var \Shop\AdminBundle\Entity\ShopRepository $repoShop */
            $repoShop=$this->getRepo('shop');

            /**@var \Shop\AdminBundle\Entity\ProductRepository $repoProd */
            $repoProd=$this->getRepo('product');

            $prod=$repoProd->getAvailableById($prodId);


            if(empty($prod)){
                return new Response('对应id的商品未找到');
            }

            $shop=$repoShop->find($prod->getShopId());

//            dump($shop);exit;

            return [
                'prod'=>$prod,
                'shop'=> $shop
            ];

        }

    }



}
